<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminLog;
use App\Model\DiamondRecord;
use App\Model\Number;
use App\Model\OperationLogs;
use App\Model\PearlRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class PearlRecordController extends BaseController
{
    public function __construct(Request $request, PearlRecord $pearlRecord)
    {
        parent::__construct($request);
        $this->model = $pearlRecord;
    }

    /**
     * 钻石流水列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return $this->toJson(self::SUCCESS, $this->model->getList($request));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Number $number)
    {
        $validator = Validator::make($request->all(),[
            'user_id' => 'bail|required|exists:api.user|integer',
            'uuid' => 'bail|required|exists:api.user|string',
            'type' => 'bail|required|integer',Rule::in(1,2),
            'change_diamond' => 'bail|required|integer|min:1|max:99999999',
            'sys_notice' => 'required_if:type,2|string'
        ],[
            'user_id.required' => '用户不可为空',
            'user_id.exists' => '用户不存在',
            'type.required' => '商品类型不可为空',
            'change_diamond.required' => '金额不可为空',
            'change_diamond.integer' => '金额必须为整数',
            'change_diamond.min' => '金额最小值为1',
            'change_diamond.max' => '金额最大值为99999999',
            'sys_notice.required_if' => '系统通知不可为空'
        ]);

        if($validator->fails()){
            return $this->toJson(self::PARAM_ERROR, [],$validator->errors()->all()[0]);
        }

        $changeDiamond = 0;
        $desc = '';
        if($request->input('type') === 1){
            $changeDiamond = '-'. $request->input('change_diamond');
            $desc = '扣款';
        } elseif ($request->input('type') === 2) {
            $changeDiamond = $request->input('change_diamond');
            $desc = '赠送';
        }
        $res = $this->remoteSetDiamond($changeDiamond, $request->input('user_id'));
        if($res['code'] === 0) {
            $number = $number->getNicknameByUserId($request->input('user_id'));

            $diamondRecord = $this->model->createData($request, $changeDiamond,$number['nickname']);
            if($request->input('type') == 2) Artisan::call('send:custom:msg',[
                'type' => 0,
                'all' => 0,
                'msg' => $request->input('sys_notice'),
                'user' => $request->input('uuid')
                ]);
            OperationLogs::createData($request, OperationLogs::DIAMOND_RECORD, $desc,$request->input('uuid'));
            return $this->toJson(self::SUCCESS, $diamondRecord);
        }
        return $this->toJson(self::ERROR,[],'所扣金额大于余额');
    }

    private function remoteSetDiamond(int $value, int $user_id){
        return curl_request(getKkaudioApiUrl().'/inner/finance/back/pearl/pay',[ 'user_id' => $user_id,'pearl_balance' => $value ],'json','POST');
    }
}
